'use strict';
const db = uniCloud.database();
const userDB = db.collection('Accounts')
exports.main = async (event, context) => {
	const {
		name,
		password
	} = event;
	console.log('event : ', event);
	if (!name || typeof name !== 'string') {
		return {
			code: 400,
			message: '用户名无效'
		};
	}
	if (!password || typeof password !== 'string') {
		return {
			code: 400,
			message: '密码无效'
		};
	}
	//event为客户端上传的参数

	try {
		let result = await userDB.where({
			name: name,
			password: password
		}).get();
		console.log("result" + JSON.stringify(result));
		if (result.data.length == 0) {

			return {
				code: 300,
				data: "用户不存在"
			};
		}
		if (result.data.length > 1) {
			console.log("result" + JSON.stringify(result));
			return {
				code: 600,
				data: "服务数据异常"
			};
		}
		let userPWD = result.data[0].password;
		if (password === userPWD) {
			return {
				code: 200,
				data: result.data[0]._id
			};
		} else {
			return {
				code: 201,
				data: "密码错误"
			};
		}

	} catch (error) {
		console.log("数据查询失败", error);
		return {
			code: 500,
			data: "服务器查询异常"
		};
	}


};